home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2007 December / PCWKCD1207B.iso / Blogowanie poza sfera / Scribefire-1.4.2 / scribefire-1.4.2-fx+fl.xpi / chrome / content / notes.js < prev    next >
Encoding:
JavaScript  |  2007-02-24  |  20.6 KB  |  500 lines

  1. /*
  2. Note Handling code
  3. ---------------------------------------------------
  4. */
  5. var gGlobalPerFormancingNotes = null;
  6. function performancingNotes() {
  7.     this.notesFile = "performancing-notes.xml";
  8. }
  9.  
  10. performancingNotes.prototype.init = function(){
  11.     this.loadNotes(false, null);
  12.     window.setTimeout('gPerformancingNotes.onSavedLoad()', 500, true);
  13. }
  14.  
  15. performancingNotes.prototype.doNoteOnUnload = function(){
  16.     //alert("Unload");
  17.     var doTheUnload = gPerformancingUtil.prefs.getBoolPref("settings.saveonexit");
  18.     if(doTheUnload){
  19.         try{
  20.             var file = this.getXMLFile();
  21.             var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  22.             var theNotesXML = new XML(theXMLString);
  23.             
  24.             var tabbox = document.getElementById("performancing-editor-tabbox");
  25.             var theContent = null;
  26.             var theTitle = document.getElementById("performancing-editor-subject").value;
  27.             if(tabbox.selectedIndex == 0 || tabbox.selectedIndex == 2){//Rich or preview
  28.                 var winNormal = document.getElementById("performancing-message");
  29.                 theContent = winNormal.contentWindow.document.body.innerHTML;
  30.             }else if(tabbox.selectedIndex == 1){ //Source
  31.                 theContent = document.getElementById("performancing-message-source").value
  32.             }else{
  33.                 //alert('PerFormancing Error: Could not find Note content, please contact the author');
  34.             }
  35.             
  36.             theNotesXML.lastsaved.note = 
  37.                                     <note> 
  38.                                     <title>{theTitle}</title> 
  39.                                     <content>{theContent}</content> 
  40.                                     </note>;
  41.             //Save the note
  42.             var newWrite = PerFormancingFileIO.write(file, theNotesXML.toString(), "w", "UTF-8");
  43.             if(!newWrite) {
  44.                 //Can not write to file error #1, you may not have the correct permissions
  45.                 var localeString = performancingUI.getLocaleString('cannotwritetofile', []);
  46.                 alert(localeString);
  47.             }
  48.         }catch(e){
  49.             //Foo
  50.         }
  51.     }
  52. }
  53.  
  54. performancingNotes.prototype.onSavedLoad = function(){
  55.         var file = this.getXMLFile();
  56.         var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  57.         var theNotesXML = new XML(theXMLString);
  58.         var theEntry = theNotesXML.lastsaved.note;
  59.         
  60.         //First Focus the Source
  61.         var tabbox = document.getElementById("performancing-editor-tabbox"); 
  62.         //tabbox.selectedIndex = 1;
  63.         
  64.         //convert back to html
  65.         var theContent = theEntry.content.toString();
  66.         theContent = theContent.replace(/</gi, "<");
  67.         theContent = theContent.replace(/>/gi, ">");
  68.         
  69.         //Then Load the contents
  70.         //var tabbox = document.getElementById("performancing-editor-tabbox");
  71.         if(tabbox.selectedIndex == 0 || tabbox.selectedIndex == 2){//Rich or preview
  72.             var winNormal = document.getElementById("performancing-message");
  73.             winNormal.contentWindow.document.body.innerHTML = theContent;
  74.         }else if(tabbox.selectedIndex == 1){ //Source
  75.             document.getElementById("performancing-message-source").value = theContent;
  76.         }else{
  77.             var localeString = performancingUI.getLocaleString('notenotfound', []);
  78.             alert(localeString);
  79.         }
  80.         var winPreview = document.getElementById("performancing-preview-display");
  81.         //winPreview.contentWindow.document.body.innerHTML = theContent;
  82.         document.getElementById("performancing-message-source").value = theContent;
  83.         performancingMidas.syncNormalTab();
  84.         
  85.         
  86.         //Then Load the Title
  87.         var theNoteName = theEntry.title.toString();
  88.         document.getElementById("performancing-editor-subject").value = theEntry.title.toString();
  89.         
  90.         performancingUI.onLoadInsert();
  91. }
  92.  
  93. performancingNotes.prototype.loadNotes = function(isSearch, searchArray){
  94.     try{
  95.         var file = this.getXMLFile();
  96.         var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  97.         var theNotesXML = new XML(theXMLString);
  98.         
  99.         var allTheNotes = theNotesXML.notes;
  100.         gGlobalPerFormancingNotes = allTheNotes;
  101.         
  102.         //allTheNotes.note.length()
  103.         //Show only search results!
  104.         //alert("isSearch: " + isSearch + " searchArray: " +searchArray);
  105.         if(isSearch){
  106.             allTheNotes = new XML(<notes></notes>);
  107.             for( l = 0; l < searchArray.length; l++){
  108.                 allTheNotes.note += theNotesXML.notes.note[ searchArray[l] ] ; 
  109.             }
  110.         }
  111.         //alert("allTheNotes: " + allTheNotes.toString() );
  112.         //gGlobalPerFormancingNotes = allTheNotes;
  113.         var noteName = null;
  114.         var noteContent = null;
  115.         var noteDate = null;
  116.         var noteGUID = null;
  117.         var onNoteClick = "gPerformancingNotes.addNoteToEditor(this);";
  118.         var theClass = "performancenote";
  119.         var isFirstRun = gPerformancingUtil.prefs.getBoolPref("firstrun");
  120.         var numOfNotes = allTheNotes.children().length();
  121.         //Load the notes into the note list
  122.         if(numOfNotes > 0){
  123.             gPerformancingUtil.clearListOut('performancing-notes-list');
  124.             for(var j = 0; j < allTheNotes.children().length(); j++){
  125.                 noteName = allTheNotes.note[j].title.toString();
  126.                 //Remove any extra spaces (atleast 2 or more consecutive spaces)
  127.                 noteName = noteName.replace(/\s\s+/gi, " ");
  128.                 if(noteName == "" || noteName == " "){
  129.                     noteName = "Untitled";
  130.                 }
  131.                 noteContent = allTheNotes.note[j].content.toString();
  132.                 noteDate = allTheNotes.note[j].datemodified.toString();
  133.                 noteGUID = allTheNotes.note[j].GUID.toString();
  134.                 this.addItemToList('performancing-notes-list', noteName, noteContent, noteDate, noteGUID, onNoteClick, j, theClass )
  135.             }
  136.         }else if(numOfNotes == 0 && isFirstRun){
  137.             //Foo.
  138.             gPerformancingUtil.clearListOut('performancing-notes-list');
  139.             this.addItemToList('performancing-notes-list', 'No notes available.', "", "", "", "", 0, "" )
  140.         }else if(!isSearch || !isFirstRun){
  141.             var formatedExample = "This is an <span style=\"font-weight: bold; color: rgb(255, 0, 0);\">example </span>of a <span style=\"font-weight: bold;\">formatted <span style=\"color: blue;\">note</span></span> note";
  142.             this.generateXML("Example Note 1", formatedExample, null, null, onNoteClick);
  143.             this.generateXML("Example Note 2", "This is an example of a normal text note", null, null, onNoteClick);
  144.             isFirstRun = true;
  145.             gPerformancingUtil.prefs.setBoolPref("firstrun", true);
  146.             this.loadNotes(false, null);
  147.         }
  148.     }catch(e){
  149.         alert("Error Loading notes: " + e);
  150.     }
  151.     //dnote.notes.children().length()
  152. }
  153.  
  154. //theNoteName, theNoteContent, aNoteDate, theGUID
  155. performancingNotes.prototype.addItemToList = function(listIDname, theNoteName, theNoteContent, aNoteDate, theGUID, onItemClick, aNumber, theClass){
  156.     var list = document.getElementById(listIDname);
  157.     var item = document.createElement('listitem');
  158.     item.setAttribute('id', 'note-' + aNumber);
  159.     item.setAttribute('noteGUID', theGUID);
  160.     item.setAttribute('tooltiptext', theNoteName);
  161.     item.setAttribute('date', aNoteDate);
  162.     item.setAttribute('onclick', onItemClick);
  163.     item.setAttribute('class', theClass);
  164.     
  165.     var itemCell = document.createElement('listcell');
  166.     itemCell.setAttribute('label', theNoteName);
  167.     itemCell.setAttribute('crop', 'right');
  168.     itemCell.setAttribute('class', theClass + '2');
  169.     item.appendChild(itemCell);
  170.     
  171.     //Append the elements
  172.     list.appendChild(item);
  173.     //sizeToContent();
  174. }
  175.  
  176. performancingNotes.prototype.doSave = function(theElement){
  177.     //Check if note doesn't already exists (same GUID)
  178.     var theNoteGUID = theElement.getAttribute("lastpostid");
  179.     var theTitle = document.getElementById("performancing-editor-subject").value;
  180.     
  181.     var file = this.getXMLFile();
  182.     var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  183.     var theNotesXML = new XML(theXMLString);
  184.     var theEntry = theNotesXML.notes.note.(title.toString() == theTitle);
  185.     if( theEntry.length() > 0 ){
  186.         //We have a match, prompt user and make sure he wants to save it.
  187.         //Call some function here
  188.         this.promptSaveType(theTitle, theElement);
  189.     }else{
  190.         //Note doesn't exists, so go save it as a new one.
  191.         gPerformancingNotes.reSaveNote(theElement, true);
  192.     }
  193.     
  194. }
  195.  
  196. //Not used anymore! (?)
  197. performancingNotes.prototype.promptSaveType = function(aTitle, theElement){
  198.     //If not, then create the new GUID
  199.     var localeString = performancingUI.getLocaleString('notealreadyexists', [aTitle]);
  200.     var localeString2 = performancingUI.getLocaleString('overwritenote', []);
  201.     if( confirm(localeString + "\n" + localeString2) ){
  202.         gPerformancingNotes.reSaveNote(theElement, false, aTitle);
  203.     }
  204. }
  205.  
  206. performancingNotes.prototype.reSaveNote = function(theElement, creatNewNote, aTitle){
  207.     //Check if note doesn't already exists (same GUID)
  208.     var theNoteGUID = theElement.getAttribute("lastpostid");
  209.     var file = this.getXMLFile();
  210.     var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  211.     var theNotesXML = new XML(theXMLString);
  212.     var theEntry = null;
  213.     try{
  214.         theEntry = theNotesXML.notes.note.(title.toString() == aTitle );
  215.     }catch(e){
  216.         //foo
  217.     }
  218.     
  219.     var tabbox = document.getElementById("performancing-editor-tabbox");
  220.     var theContent = null;
  221.     var theTitle = document.getElementById("performancing-editor-subject").value;
  222.     if(tabbox.selectedIndex == 0 || tabbox.selectedIndex == 2){//Rich or preview
  223.         var winNormal = document.getElementById("performancing-message");
  224.         theContent = winNormal.contentWindow.document.body.innerHTML;
  225.     }else if(tabbox.selectedIndex == 1){ //Source
  226.         theContent = document.getElementById("performancing-message-source").value
  227.     }else{
  228.         var localeString = performancingUI.getLocaleString('notenotfound', []);
  229.         alert(localeString);
  230.     }
  231.     
  232.     var theNoteDate = bfXMLRPC.iso8601Format( new Date() );
  233.     if( theEntry.length() > 0  && !creatNewNote){
  234.         //Save new content and
  235.         var theIndex = theEntry.childIndex();
  236.         theNotesXML.notes.note[theIndex].title = theTitle;
  237.         theNotesXML.notes.note[theIndex].content = theContent;
  238.         theNotesXML.notes.note[theIndex].datemodified = theNoteDate;
  239.         
  240.         //Save the note
  241.         var newWrite = PerFormancingFileIO.write(file, theNotesXML.toString(), "w", "UTF-8");
  242.         if(!newWrite) {
  243.             var localeString = performancingUI.getLocaleString('cannotwritetofile', []);
  244.             alert(localeString);
  245.         }
  246.     }else{
  247.         //If not, then create the new GUID
  248.         var theGUID = this.newGuid();
  249.         //Not will be saved by this function
  250.         result = this.generateXML( theTitle, theContent, theNoteDate, theGUID );
  251.         
  252.     }
  253.     //Remove re-save button
  254.     var noteButton = document.getElementById("performancing-resavenote-button");
  255.     noteButton.hidden = true;
  256.     
  257.     //Now reload notes
  258.     gPerformancingNotes.loadNotes(false, null);
  259. }
  260.  
  261. performancingNotes.prototype.generateXML = function(theNoteName, theNoteContent, aNoteDate, theGUID){
  262.     //We need to get this from a file and theBlogXML = new XML(theFilesStringContents);
  263.     var file = this.getXMLFile();
  264.     var theXMLFile = null;
  265.     var theBlogXML = null;
  266.     var theNoteDate = null;
  267.     if(!aNoteDate){
  268.         theNoteDate = bfXMLRPC.iso8601Format( new Date() );
  269.     }
  270.     
  271.     if(!theGUID){
  272.         theGUID = this.newGuid();
  273.     }
  274.     //Doesn't exist, so let's create it.
  275.     if(!file.exists()){
  276.         //dump('Creating file: ' + bloglistxmlfile + "\n");
  277.         theBlogXML = <notelist>
  278.                         <lastsaved>
  279.                             <note></note>
  280.                         </lastsaved>
  281.                         <notes>
  282.                         </notes>
  283.                      </notelist>;
  284.         PerFormancingFileIO.create(file);
  285.         var rv = PerFormancingFileIO.write(file, theBlogXML.toString(), "w", "UTF-8");
  286.         if(!rv) {
  287.             var localeString = performancingUI.getLocaleString('cannotwritetofile', []);
  288.             alert(localeString);
  289.         }
  290.     }
  291.     
  292.     if(file.exists()){
  293.         var theXMLFile = PerFormancingFileIO.open(file.path);
  294.         var theXMLString = PerFormancingFileIO.read(theXMLFile, "UTF-8");
  295.         theBlogXML = new XML(theXMLString);
  296.         
  297.         //The Content for each Blog
  298.         theBlogXML.notes.note +=
  299.                                     <note>
  300.                                         <title>{theNoteName}</title>
  301.                                         <content>{theNoteContent}</content>
  302.                                         <datemodified>{theNoteDate}</datemodified>
  303.                                         <GUID>{theGUID}</GUID>
  304.                                     </note>;
  305.         //Write to file (save changes)
  306.         var newWrite = PerFormancingFileIO.write(file, theBlogXML.toString(), "w", "UTF-8");
  307.         if(!newWrite) {
  308.             var localeString = performancingUI.getLocaleString('cannotwritetofile', []);
  309.             alert(localeString);
  310.         }
  311.                                 
  312.         return theBlogXML;
  313.     }else{
  314.         return false;
  315.     }
  316. }
  317.  
  318. performancingNotes.prototype.addNoteToEditor = function(theElement){
  319. /*
  320.    Todo:
  321.    - Load Content
  322.    - Load Subject
  323.    - Load Categories
  324.    - Display 'Save as New' and 'Save'
  325. */
  326.     var theNoteGUID = theElement.getAttribute("noteGUID");
  327.     var file = this.getXMLFile();
  328.     var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  329.     var theNotesXML = new XML(theXMLString);
  330.     
  331.     try{
  332.         //dump("The Read NOTES XML: " + theNotesXML.notes.note + '\n' + "aPostId to get:" + theNoteGUID + '\n')
  333.         var theEntry = theNotesXML.notes.note.(GUID == theNoteGUID );
  334.         
  335.         //Now load everything
  336.         //First Focus the Source
  337.         var tabbox = document.getElementById("performancing-editor-tabbox"); 
  338.         //tabbox.selectedIndex = 1;
  339.         
  340.         //convert back to html
  341.         var theContent = theEntry.content.toString();
  342.         theContent = theContent.replace(/</gi, "<");
  343.         theContent = theContent.replace(/>/gi, ">");
  344.         
  345.         //Then Load the contents
  346.         //var tabbox = document.getElementById("performancing-editor-tabbox");
  347.         if(tabbox.selectedIndex == 0 || tabbox.selectedIndex == 2){//Rich or preview
  348.             var winNormal = document.getElementById("performancing-message");
  349.             winNormal.contentWindow.document.body.innerHTML = theContent;
  350.         }else if(tabbox.selectedIndex == 1){ //Source
  351.             document.getElementById("performancing-message-source").value = theContent;
  352.         }else{
  353.             var localeString = performancingUI.getLocaleString('notenotfound', []);
  354.             alert(localeString);
  355.         }
  356.         var winPreview = document.getElementById("performancing-preview-display");
  357.         //winPreview.contentWindow.document.body.innerHTML = theContent;
  358.         document.getElementById("performancing-message-source").value = theContent;
  359.         performancingMidas.syncNormalTab();
  360.         
  361.         
  362.         //Then Load the Title
  363.         var theNoteName = theEntry.title.toString();
  364.         document.getElementById("performancing-editor-subject").value = theEntry.title.toString();
  365.         
  366.         //Show Republish button and set attributes
  367.         var pubButton = document.getElementById("performancing-resavenote-button");
  368.         pubButton.setAttribute("lastpostid", theNoteGUID);
  369.         pubButton.hidden = false;
  370.         
  371.         return true;
  372.     }catch(e){
  373.         var localeString = performancingUI.getLocaleString('noteerror', [e]);
  374.         alert(localeString);
  375.         return true;
  376.     }
  377.     
  378. }
  379.  
  380. performancingNotes.prototype.deleteNoteEntry = function(){
  381.     try{
  382.         var resaveButton = document.getElementById("performancing-resavenote-button");
  383.         var theNoteGUID = resaveButton.getAttribute("lastpostid");
  384.         var file = this.getXMLFile();
  385.         var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  386.         var theNotesXML = new XML(theXMLString);
  387.         //delete dude..blog.(GUID == theGUID);
  388.         var entryIndex = theNotesXML.notes.note.(GUID == theNoteGUID ).childIndex();
  389.         delete theNotesXML.notes.note[entryIndex];
  390.         
  391.         //Now save the change
  392.         var rv = PerFormancingFileIO.write(file, theNotesXML.toString(), "w", "UTF-8");
  393.         if(!rv) {
  394.             var localeString = performancingUI.getLocaleString('cannotwritetofile', [e]);
  395.             alert(localeString);
  396.         }
  397.     }catch(e){
  398.         var localeString = performancingUI.getLocaleString('cantdeletenote', []);
  399.         //Could not delete the selected note!
  400.         alert(localeString);
  401.     }
  402.     //Now reload notes
  403.     performancingMidas.makeBlank();
  404.     performancingUI.toggleExtraOptDeck(true);
  405.     performancingUI.clearTechnoratiTags();
  406.     gPerformancingNotes.loadNotes(false, null);
  407. }
  408.  
  409. performancingNotes.prototype.getXMLFile = function(){
  410.     var file = PerFormancingDirIO.get("ProfD");
  411.     file.append("extensions");
  412.     file.append(this.notesFile);
  413.     return file;
  414. }
  415.  
  416. //Return array of index's of notes or false if not found
  417. performancingNotes.prototype.searchNotes = function(theTextString){
  418.     //First search in title
  419.     theTextString = theTextString.toLowerCase();
  420.     var file = this.getXMLFile();
  421.     var theXMLString = PerFormancingFileIO.read(file, "UTF-8");
  422.     var theNotesXML = new XML(theXMLString);
  423.     
  424.     var allTheNotes = theNotesXML.notes;
  425.     
  426.     var theNoteTitles = allTheNotes.note.title;
  427.     var theNoteContent = allTheNotes.note.content;
  428.     
  429.     var noteLength = theNoteTitles.length();
  430.     var searchResults = [];
  431.     
  432.     for(i=0; i < noteLength; i++){
  433.         var theTitle = "";
  434.         var theContent = "";
  435.         try{
  436.             theTitle = theNoteTitles[i].text().toLowerCase();
  437.         }catch(e){}
  438.         try{
  439.             theContent = theNoteContent[i].text().toLowerCase();
  440.         }catch(e){}
  441.         if( theTitle.search(theTextString) >= 0 ){
  442.             searchResults.push( i ) ;
  443.         }else if( theContent.search(theTextString) >= 0 ){
  444.             searchResults.push( i ) ;
  445.         }
  446.     }
  447.  
  448.     if(searchResults.length > 0 ){
  449.         return searchResults;
  450.     }else{
  451.         //didn't find anything!
  452.         return false;
  453.     }
  454.     //didn't find anything!
  455.     return false;
  456.     
  457.     //then search in content if that failed
  458.     
  459. }
  460.  
  461. //for search
  462. performancingNotes.prototype.onNoteSearchChange = function(){
  463.     var theTextString = document.getElementById("performancing-search-notes-text").value;
  464.     var notesToShow = gPerformancingNotes.searchNotes( theTextString );
  465.     
  466.     if(!notesToShow){
  467.         gPerformancingUtil.clearListOut('performancing-notes-list');
  468.         this.addItemToList('performancing-notes-list', 'No results found', '', '', '', '', 0, 'onsearcherror' )
  469.     }else{
  470.         gPerformancingNotes.loadNotes(true, notesToShow );
  471.     }
  472.     
  473. }
  474.  
  475. /*
  476. // Original function by Lewis E. Moten III
  477. // Terms of Agreement:
  478. // By using this code, you agree to the following terms...
  479. // 1) You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.   
  480. // 2) You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  481. // 3) You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  482. // 4) You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
  483. */
  484. performancingNotes.prototype.newGuid = function(){
  485.     var g = ""; 
  486.     for(var i = 0; i < 32; i++){
  487.         g += Math.floor(Math.random() * 0xF).toString(0xF) + (i == 8 || i == 12 || i == 16 || i == 20 ? "-" : ""); 
  488.     }
  489.     return g;
  490. }
  491. ////////////////////
  492.  
  493. var gPerformancingNotes = null;
  494. function loadPerFormancingNotes() {
  495.     try {
  496.         gPerformancingNotes = new performancingNotes();
  497.     } catch(e) { alert(e); }
  498.     gPerformancingNotes.init();//Load (D&D) Prefs
  499. }
  500.